<?php 

	session_start();
	$userID = $_SESSION['userId'];

	
	$con = mysql_connect("localhost", "group5", "group5");
    if(!$con)
    {
		echo "Connection failed!";
        exit;
    }

	mysql_select_db("group5");

	$meetdate = $_POST['meetdate'];
	$meetdate = explode('/',$meetdate);
	$meetdate = "'" . $meetdate[2] . "-" . $meetdate[0] . "-" . $meetdate[1] . "'";
	$meettime = $_POST['meetingtime'];
	$building = $_POST['building'];
	$capacity = $_POST['capacity'];
	
	$room = $_POST['room'];
	$workshop = $_POST['category'];
	
	
	$table = mysql_query("select * from ROOM where roomNumber = $room");
	
	//Found the room in the table
	if($row = mysql_fetch_row($table))
	{
		$roomId = $row[0];
		
		$meettime = "'" . $meettime . "'";
		
		$query = "INSERT INTO WORKSHOP_SESSIONS (meetingTime, meetingDay, workshopId, roomId, instructorId)  
		VALUES ($meettime, $meetdate, $workshop, $roomId, $userID)";
		echo $query;
	}
	//Room is not yet in table.
	else
	{
		//Get the buildingID
		$table = mysql_query("select * from BUILDING where bldgNum = $building");
		$row = mysql_fetch_row($table);
		$buildingId = $row[0];
		
		//Create new roomId
		$new = "INSERT INTO ROOM (maxCapacity, roomNumber, bldgID) VALUES($capacity, $room, $buildingId)";
		mysql_query($new, $con);
		
		//Get the new RoomId
		$table = mysql_query("select * from ROOM where roomNumber = $room");
		$row = mysql_fetch_row($table);
		$roomId = $row[0];
		
		$meettime = "'" . $meettime . "'";
		$query = "INSERT INTO WORKSHOP_SESSIONS (meetingTime, meetingDay, workshopId, roomId, instructorId)  
		VALUES ($meettime, $meetdate, $workshop, $roomId, $userID)";
		
	}
	
	
	mysql_query($query, $con);
	header("Location:adminManage.php");
?>
